﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BanVeXemPhim_Presentation
{
    public class RapChieuDAO:DataProvider
    {
        public RapChieuDTO[] DanhSachRapChieu()
        {
            connect();
            string query = "select * from rapchieu";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dataTable = dataset.Tables[0];
            int n = dataTable.Rows.Count;
            RapChieuDTO[] dsRap = new RapChieuDTO[n];
            for (int i = 0; i < n; i++)
            {
                object rap = GetDataFromDataRow(dataTable, i);
                dsRap[i] = (RapChieuDTO)rap;
            }
            return dsRap;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            RapChieuDTO rapChieuDto=new RapChieuDTO();
            rapChieuDto.IdRapChieu = Convert.ToInt32(dt.Rows[i]["IdRapChieu"].ToString());
            rapChieuDto.TenRap = dt.Rows[i]["TenRap"].ToString();
            rapChieuDto.ViTri = dt.Rows[i]["ViTri"].ToString();
            rapChieuDto.TinhTrang = Convert.ToInt32(dt.Rows[i]["TinhTrang"].ToString());
            rapChieuDto.SoHang = Convert.ToInt32(dt.Rows[i]["SoHang"].ToString());
            rapChieuDto.SoCot = Convert.ToInt32(dt.Rows[i]["SoCot"].ToString());
            return rapChieuDto;
        }

        public void ThemRapChieu(RapChieuDTO rapchieuInfo)
        {
            connect();
            string query = "insert into rapchieu(TenRap,ViTri,TinhTrang,SoHang,SoCot) values(N'" + 
                rapchieuInfo.TenRap + "',N'" + 
                rapchieuInfo.ViTri + "'," + 
                rapchieuInfo.TinhTrang.ToString() + ","+
                rapchieuInfo.SoHang+"," + 
                rapchieuInfo.SoCot + ")";
            command = connection.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
            connection.Close();

        }
        public int LayIdRapChieu(RapChieuDTO rapchieuInfo)
        {
            connect();
            string query =
                "select IdRapChieu from rapchieu where TenRap=N'"+rapchieuInfo.TenRap.ToString()+"' and ViTri=N'"+rapchieuInfo.ViTri.ToString()+"' and TinhTrang="+rapchieuInfo.TinhTrang.ToString()+" and SoHang="+rapchieuInfo.SoHang.ToString()+" and SoCot="+rapchieuInfo.SoCot.ToString();
            command = connection.CreateCommand();
            command.CommandText = query;
            object o = new object();
            o = command.ExecuteScalar();
            if (o == null)
            {
                return -1;
            }
            return (int)o;
        }
        public void CapNhatThongTinRapChieu(RapChieuDTO rapchieuInfo)
        {
            connect();
            string query = "update rapchieu set TenRap=N'"+
                rapchieuInfo.TenRap+"',ViTri=N'"+
                rapchieuInfo.ViTri + "',TinhTrang=" +
                rapchieuInfo.TinhTrang.ToString() + ",SoHang=" +
                rapchieuInfo.SoHang.ToString() + ",SoCot=" +
                rapchieuInfo.SoCot.ToString() + "where IdRapChieu=" +
                rapchieuInfo.IdRapChieu.ToString();
            command = connection.CreateCommand();
            command.CommandText = query;
            command.ExecuteNonQuery();
        }
    }
    

}
